<!--
@license
Copyright 2018 The Tensor2Tensor Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../polymer/polymer.html">

<link rel="import" href="../iron-ajax/iron-ajax.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout-classes.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="../iron-icons/av-icons.html">
<link rel="import" href="../iron-icons/iron-icons.html">

<link rel="import" href="../paper-button/paper-button.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../paper-slider/paper-slider.html">
<link rel="import" href="../paper-tooltip/paper-tooltip.html">

<dom-module id="graph-visualization">
  <template>
    <style include="iron-flex iron-flex-alignment">
        #chart {
          border: 1px #ccc solid;
          position: relative;
        }

        #help {
          position: absolute;
          top: 10px;
          right: 10px;
          z-index: 1;
        }

        .background {
          fill: #eee;
        }

        line {
          stroke: #000;
          stroke-width: 1.5px;
        }

        rect {
          fill: transparent;
        }

        .node circle {
          cursor: pointer;
          fill: #fff;
          stroke: steelblue;
          stroke-width: 1.5px;
        }

        g.selected circle {
          fill: lightsteelblue;
        }

        .node text {
          font-size: 12px;
        }

        path.link {
          fill: none;
          stroke-width: 1.5px;
        }

        circle.terminal {
          fill: lightsteelblue;
        }

        circle.nonterminal {
          fill: #fff;
        }

        text {
          fill: #222;
        }

        g.fade circle,
        g.fade text {
          opacity: 0.1;
        }

        .svg-container {
          display: inline-block;
          position: relative;
          width: 100%;
          padding-bottom: 100%;
          vertical-align: top;
          overflow: hidden;
        }

        .svg-content-responsive {
          display: inline-block;
          position: absolute;
          top: 10px;
          left: 0px;
        }

        #info {
          background: #fff;
          border: 1px solid #bce8f1;
          border-radius: 5px;
          position: absolute;
          right: 12px;
          top: 12px;
          z-index: 10;
        }

        #info .header {
          background-color: #bce8f1;
          border-color: #bce8f1;
          color: #31708f;
        }

        #info .header,
        #info .details {
          padding: 12px 6px;
        }
    </style>
    <div class="layout horizontal">
      <paper-icon-button id="home" on-tap="reset_" icon=home></paper-icon-button>
      <paper-slider id="slider" min=0 max=100 value="{{zoomDepth_}}"></paper-slider>
      <div class="flex"></div>
      <iron-pages selected="[[stepMode]]" attr-for-selected="name">
        <div name="view">
          <paper-button raised on-tap="startStepMode_">Start Step Decoding</paper-button>
        </div>
        <div name="edit">
          <paper-icon-button on-tap="step_" icon=av:play-arrow></paper-icon-button>
          <paper-button raised on-tap="exitStepMode_">Exit Step Decoding</paper-button>
        </div>
      </iron-pages>
    </div>
    <div id="chart">
      <div id="info">
        <div class="header">
          Node Details
        </div>
        <div class="details">
          <div>Token: <span>[[currentName]]</span></div>
          <div>Token Probability: <span>[[currentProbability]]</span></div>
          <div>Total Probability: <span>[[currentTotalProbability]]</span></div>
          <div>Score: <span>[[score]]</span></div>
        </div>
      </div>
    </div>

    <iron-ajax
      id="startAjax"
      url="/api/remote_decoder_start"
      method="POST"
      body="[[startBody]]"
      handle-as="json"
      on-error="handleStartError_"
      on-response="handleStartResponse_"
      last-response="{{startResponse_}}">
    </iron-ajax>

    <iron-ajax
      id="generateAjax"
      url="/api/remote_decoder_generate"
      method="POST"
      body="[[generateBody]]"
      params="[[generateParams]]"
      handle-as="json"
      on-error="handleGenerateError_"
      on-response="handleGenerateResponse_"
      last-response="{{generateResponse_}}">
    </iron-ajax>

  </template>
  <script src="graph-visualization.js"></script>
</dom-module>

